package com.atguigu.springcloud.edu.controller;


import com.atguigu.common.CommonResult;
import com.atguigu.entity.Payment;

import com.atguigu.springcloud.edu.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-11
 */
@RestController
@RequestMapping("/edu/payment")
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    PaymentService paymentService;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping(value = "/payment/discovery")
    public void discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(element);
            for (int i = 0; i < instances.size(); i++) {
                System.out.println(instances.get(i).getServiceId());
                System.out.println(instances.get(i).getHost());
                int port = instances.get(i).getPort();
                System.out.println(port);

            }
        }

    }

    @GetMapping("selectall")
    public CommonResult<Payment> select(){
        List<Payment> list = paymentService.list();
        return new CommonResult(200,list);
    }
    @GetMapping("selectById/{id}")
    public CommonResult<Payment> selectById(@PathVariable Long id){
        Payment payment = paymentService.getById(id);
        return new CommonResult(200,payment);

    }



    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout()
    {
        // 业务逻辑处理正确，但是需要耗费3秒钟
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }




}

